'''
@Author: your name
@Date: 2020-02-03 12:15:10
@LastEditTime : 2020-02-03 15:27:31
@LastEditors  : Please set LastEditors
@Description: In User Settings Edit
@FilePath: \pyqt5\demo_019_父子关系.py
'''
from PyQt5.Qt import *


class Window(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('demo_019_父子关系')
        self.resize(500,500)
        self.setup_ui()

    def setup_ui(self):
        self.QObject父子关系()

    def QObject父子关系(self):
        obj_parent = QObject()
        obj_child_1 = QObject()
        obj_child_2 = QObject()
        obj_child_3 = QObject()
        obj_child_4 = QObject()
        obj_child_5 = QObject()
        # 设置objectname
        obj_child_1.setObjectName('obj_child_1')
        obj_child_2.setObjectName('obj_child_2')
        obj_child_3.setObjectName('obj_child_3')
        obj_child_4.setObjectName('obj_child_4')
        obj_child_5.setObjectName('obj_child_5')
        # 设置父子关系 
        obj_child_1.setParent(obj_parent)
        obj_child_2.setParent(obj_parent)
        obj_child_3.setParent(obj_child_1)
        obj_child_4.setParent(obj_child_2)
        obj_child_5.setParent(obj_child_2)


        #
        
        print(obj_parent)
        print(obj_child_1)
        print(obj_child_2)
        print(obj_child_3)
        print(obj_child_4)
        print(obj_child_5)

        # 1 parent()  获取所有的父对象
        print('get child_1 parent-->')
        print(obj_child_1.parent())
        # 2 children() 获取所有的直接子对象
        print('get parent children-->')
        print(obj_parent.children())
        # 3 findchild()
        print('get obj_child_2\'s children-->')
        print(obj_child_2.findChild(QObject,'obj_child_5',Qt.FindChildrenRecursively))
        # 4 findchildren()
        print('get obj_parent\'s children-->')
        print(obj_parent.findChildren(QObject))


if __name__ == '__main__':
    import sys
    app = QApplication(sys.argv)

    win = Window()
    win.show()
    sys.exit(app.exec_())
